package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;

@Service
public class AlertDataServiceImpl implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Override
    public PageResponse<AlertDataVo> pageQuery(AlertDataPageQueryDto alertDataPageQueryDto) {
        PageHelper.startPage(alertDataPageQueryDto.getPageNum(),alertDataPageQueryDto.getPageSize());
        LocalDateTime startTime = alertDataPageQueryDto.getStartTime() == null ? null : LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime());
        LocalDateTime endTime = alertDataPageQueryDto.getEndTime() == null ? null : LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime());
        Page<AlertDataVo> pageResult = alertDataMapper.adminPageQuery(alertDataPageQueryDto.getId(),alertDataPageQueryDto.getStatus(),alertDataPageQueryDto.getDeviceName(),startTime,endTime);

        return PageResponse.of(pageResult,AlertDataVo.class);
    }

    @Override
    public void handleAlertData(Long id, AlertDataHandleDto alertDataHandleDto) {
        AlertData alertData = alertDataMapper.selectByPrimaryKey(id);
        String subject = UserThreadLocal.getSubject();
        UserVo userVo = JSONUtil.toBean(subject, UserVo.class);
        alertDataMapper.handleAlertData(id,alertDataHandleDto.getProcessingResult(),alertDataHandleDto.getProcessingTime(),userVo.getRealName(),userVo.getId(),alertData.getIotId());
        alertDataMapper.updateAlertDataStatus(alertDataHandleDto.getProcessingResult(),alertDataHandleDto.getProcessingTime(),userVo.getRealName(),userVo.getId(),alertData.getIotId());
    }
}
